package com.winai.launcher.view.set;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.sdwfqin.cbt.CbtManager;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.bean.BluetoothItemBean;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.override.dialog_global.GlobalDialog;
import com.winai.launcher.utils.BlueToothUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/11/22
 * 说明：蓝牙忽略——取消配对
 */
public class IgnoreBluetoothActivity extends BaseActivity {
    private Context mContext = this;

    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.tv_bluetoothName)
    TextView tvBluetoothName;
    @BindView(R.id.tv_linkState)
    TextView tvLinkState;

    private BluetoothItemBean bluetoothBean;
    private GlobalDialog.Builder tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ignore_bluetooth);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        initView();
    }

    private void initObject() {
        bluetoothBean = getIntent().getParcelableExtra("bluetoothBean");

        tipDialog = new GlobalDialog.Builder(mContext)
                .setTitle("提示")
                .setContent("忽略设备？")
                .setConfirmText("确认")
                .setCancelText("取消")
                .setConfirmGradient(true)
                .setOnConfirmClickListener((mGlobalDialog, v) -> {
                    // 设置忽略此设备
                    BlueToothUtils.getInstance().removeBond(bluetoothBean.getDevice().getDevice());//与设备解除配对
                    BleManager.getInstance().disconnect(bluetoothBean.getDevice());
                    CbtManager.getInstance().disableCancelService();

                    GlobalPublisher.Notify(GlobalPublisher.REMOVE_BOND_BLUETOOTH, bluetoothBean.getDevice().getDevice());
                    finish();
                });
    }

    private void initView() {
        if (bluetoothBean != null) {
            strTitle.setText(bluetoothBean.getBluetoothName());
            tvBluetoothName.setText(bluetoothBean.getBluetoothName());
            tvLinkState.setText((bluetoothBean.getBluetoothState() == BluetoothItemBean.BluetoothState.LINKED ||
                    bluetoothBean.getBluetoothState() == BluetoothItemBean.BluetoothState.BONDED) ? "已连接" : "未连接");
        }
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.tv_ignore})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
            case R.id.tv_ignore:
                tipDialog.show();
                break;
        }
    }
}
